<template>
  <div class="Loign">
    <!-- 背景图 -->
    <div class="bjt">
      <img
        src="https://image.lnstzy.cn/aoaodcom/2020-01/02/2020010201145675da71e29266a760b7b7a11ccf4198d9.jpg.h700.jpg"
        alt=""
      />
    </div>
    <!-- 登录表单图片 -->
    <div class="login_img">
      <img
        src="https://c-ssl.duitang.com/uploads/item/201910/18/20191018222755_vktds.thumb.1000_0.jpg"
        alt=""
      />
    </div>
    <!-- 登录表单 -->
    <div class="logindiv">
      <el-form
        ref="loginFromRef"
        class="login_from"
        :rules="loginFromRules"
        :model="loginFrom"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginFrom.username"
            prefix-icon="iconfont icon-user-fill"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginFrom.password"
            prefix-icon="iconfont icon-zu"
          ></el-input>
        </el-form-item>
        <el-form-item class="bts">
          <el-button @click="login1" type="primary">登录</el-button>
          <el-button @click="resetLoginFrom" type="info">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// import axios from 'axios';
import { login } from "../api/Login";
export default {
  // 设置登录页不显示纵向滚动条
  beforeCreate() {
    document.querySelector("body").setAttribute("style", "overflow-y: hidden;");
  },
  //销毁前清除
  beforeDestroy() {
    document.querySelector("body").removeAttribute("style");
  },
  data() {
    return {
      // 登录表单的数据绑定对象
      loginFrom: {
        username: "",
        password: "",
      },
      // 这是表单验证规则对象
      loginFromRules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 3,
            max: 16,
            message: "长度在 3 到 16 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 16,
            message: "长度在 3 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 重置表单
    resetLoginFrom() {
      this.$refs.loginFromRef.resetFields();
    },
    // 登录
    login1() {
      this.$refs.loginFromRef.validate((valid) => {
        if (!valid) return;
        // 判断登录
        login(this.loginFrom).then((res) => {
          if (res.data == false) {
            this.$message.error("登录失败！");
          } else {
            this.$message({
              message: "登录成功！",
              type: "success",
            });
            // console.log(res, "res");
            window.sessionStorage.setItem("token", res.data == false);
            this.$router.push("/");
          }
        });
      });
    },
  },
};
</script>

<style scoped>
.login_img img {
  width: 150px;
  height: 150px;
  border-radius: 100%;
  position: absolute;
  top: 29%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1;
  box-shadow: 0 0 15px #ddd;
}
.bjt img {
  background-repeat: no-repeat;
  width: 100%;
}
.logindiv {
  position: absolute;
  border-radius: 3px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 300px;
  background: #fff;
  box-shadow: 0 0 15px #ddd;
}
.login_from {
  box-sizing: border-box;
  padding: 0 20px;
  width: 100%;
  position: absolute;
  bottom: 0;
}
.bts {
  display: flex;
  justify-content: flex-end;
}

</style>
